﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace GameNesSL.Controls
{
    public partial class ImageButton : UserControl
    {
        public static DependencyProperty disbledImageProperty = DependencyProperty.Register("DisbledImage", typeof(ImageSource), typeof(ImageButton), null);
        public ImageSource DisbledImage
        {
            get
            {
                return (ImageSource)GetValue(disbledImageProperty);
            }
            set
            {
                SetValue(disbledImageProperty, value);
            }
        }



        public static DependencyProperty pressedImageProperty = DependencyProperty.Register("PressedImage", typeof(ImageSource), typeof(ImageButton), null);
        public ImageSource PressedImage
        {
            get
            {
                return (ImageSource)GetValue(pressedImageProperty);
            }
            set
            {
                SetValue(pressedImageProperty, value);
            }
        }



        public static DependencyProperty normalImageProperty = DependencyProperty.Register("normalImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(NormalImageSet));

        private static void NormalImageSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ImageButton)d).buttonImage.Source = (ImageSource)e.NewValue;
        }

        public ImageSource NormalImage
        {
            get
            {
                return (ImageSource)GetValue(normalImageProperty);
            }
            set
            {
                SetValue(normalImageProperty, value);
            }
        }

        public static DependencyProperty hoverImageProperty = DependencyProperty.Register("hoverImage", typeof(ImageSource), typeof(ImageButton), null);
        public ImageSource HoverImage
        {
            get
            {
                return (ImageSource)GetValue(hoverImageProperty);
            }
            set
            {
                SetValue(hoverImageProperty, value);
            }
        }

        public static DependencyProperty useZoomProperty = DependencyProperty.Register("useZoom", typeof(bool), typeof(ImageButton), new PropertyMetadata(false, ZoomChanged));

        private static void ZoomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageButton btn = ((ImageButton)d);
            if ((bool)e.NewValue)
            {
                //Zoom out button
                btn.scale.ScaleX = 0.8;
                btn.scale.ScaleY = 0.8;
            }
            else
            {
                btn.scale.ScaleX = 1;
                btn.scale.ScaleY = 1;
            }
        }

        public bool UseZoom
        {
            get
            {
                return (bool)GetValue(useZoomProperty);
            }
            set
            {
                SetValue(useZoomProperty, value);
            }
        }

        public new static DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(ImageButton), new PropertyMetadata(true, EnabledChanged));

        private static void EnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                if (((ImageButton)d).NormalImage != null)
                {
                    ((ImageButton)d).buttonImage.Source = ((ImageButton)d).NormalImage;
                }
            }
            else
            {
                if (((ImageButton)d).DisbledImage != null)
                {
                    ((ImageButton)d).buttonImage.Source = ((ImageButton)d).DisbledImage;
                }
            }
        }

        public new bool IsEnabled
        {
            get
            {
                return (bool)GetValue(IsEnabledProperty);
            }
            set
            {
                SetValue(IsEnabledProperty, value);
            }
        }



        public ImageButton()
        {
            InitializeComponent();
            if (IsEnabled)
            {
                buttonImage.Source = NormalImage;
            }
            else if (DisbledImage != null)
            {
                buttonImage.Source = DisbledImage;
            }
        }

        private void buttonImage_MouseEnter(object sender, MouseEventArgs e)
        {
            if (this.IsEnabled)
            {
                if (UseZoom)
                {
                    mouseIn.Begin();
                }

                if (HoverImage != null)
                {
                    buttonImage.Source = HoverImage;
                }

            }
        }

        private void buttonImage_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this.IsEnabled)
            {
                if (UseZoom)
                {
                    mouseOut.Begin();
                }

                if (NormalImage != null)
                {
                    buttonImage.Source = NormalImage;
                }

            }
        }

        private void buttonImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.IsEnabled)
            {
                if (PressedImage != null)
                {
                    buttonImage.Source = PressedImage;
                }
                OnMouseLeftButtonDown(e);
            }
        }

        private void buttonImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.IsEnabled)
            {
                if (NormalImage != null)
                {
                    buttonImage.Source = NormalImage;
                }
                OnMouseLeftButtonUp(e);
            }
        }

        private void buttonImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }
    }
}
